package com.goodving.guli.utility;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Administrator on 2015-11-17.
 */
public class GuliActivityLifecycleCallbacks implements ActivityLifecycleCallbacks { // C1137a
    public static final String TAG = GuliActivityLifecycleCallbacks.class.getName();
    public static final long delayTime = 2000;
    private static C1135b f4578c;
    private static C1135b f4579d;
    private static GuliActivityLifecycleCallbacks mCallbacks;
    private boolean f4581f;
    private Activity mActivity;
    private C1136d f4583h;
    private Handler mHandler;
    private Runnable mRunnable;

    static {
        f4578c = new C1138b();
        f4579d = new C1139c();
    }
    
    public interface C1133c {
        void m8179a();

        void m8180b();
    }

    public interface C1134a {
        void m8185a();
    }

    private interface C1135b {
        void m8186a(C1133c c1133c);
    }

    static class C1141e implements C1134a {
        final /* synthetic */ WeakReference f4588a;
        final /* synthetic */ C1136d f4589b;

        C1141e(GuliActivityLifecycleCallbacks.C1136d c1136d, WeakReference weakReference) {
            this.f4589b = c1136d;
            this.f4588a = weakReference;
        }

        public void m8185a() {
            this.f4589b.f4575a.remove(this.f4588a);
        }
    }

    static final class C1139c implements C1135b {
        C1139c() {
        }

        public void m8186a(C1133c c1133c) {
            c1133c.m8180b();
        }
    }

    private static class C1136d {
        private List<WeakReference<C1133c>> f4575a;

        private C1136d() {
            this.f4575a = new CopyOnWriteArrayList();
        }

        public C1134a m8188a(C1133c c1133c) {
            WeakReference weakReference = new WeakReference(c1133c);
            this.f4575a.add(weakReference);
            return new C1141e(this, weakReference);
        }

        public void m8189a(C1135b c1135b) {
            Iterator it = this.f4575a.iterator();
            while (it.hasNext()) {
                try {
                    C1133c c1133c = (C1133c) ((WeakReference) it.next()).get();
                    if (c1133c != null) {
                        c1135b.m8186a(c1133c);
                    } else {
                        it.remove();
                    }
                } catch (Throwable e) {
                    Log.e(GuliActivityLifecycleCallbacks.TAG, "Listener threw exception!", e);
                }
            }
        }
    }

    public GuliActivityLifecycleCallbacks() {
        this.f4583h = new C1136d();
        this.mHandler = new Handler();
    }

    static final class C1138b implements C1135b {
        C1138b() {
        }

        public void m8186a(C1133c c1133c) {
            c1133c.m8179a();
        }
    }

    public static GuliActivityLifecycleCallbacks getInstance(Application application) { // m8191a
        if (mCallbacks == null) {
            mCallbacks = new GuliActivityLifecycleCallbacks();
            application.registerActivityLifecycleCallbacks(mCallbacks);
        }
        return mCallbacks;
    }

    public static GuliActivityLifecycleCallbacks m8194b(Application application) {
        if (mCallbacks == null) {
            GuliActivityLifecycleCallbacks.getInstance(application);
        }
        return mCallbacks;
    }

    public static GuliActivityLifecycleCallbacks getCallbacks() { // m8190a
        if (mCallbacks != null) {
            return mCallbacks;
        }
        throw new IllegalStateException("Foreground is not initialised - first invocation must use parameterised init/get");
    }

    public boolean m8196b() {
        return this.f4581f;
    }

    public boolean m8197c() {
        return !this.f4581f;
    }

    public C1134a m8195a(C1133c c1133c) {
        return this.f4583h.m8188a(c1133c);
    }

    public void onActivityResumed(Activity activity) {
    }

    class C1140d implements Runnable {
        final /* synthetic */ WeakReference f4586a;
        final /* synthetic */ GuliActivityLifecycleCallbacks f4587b;

        C1140d(GuliActivityLifecycleCallbacks c1137a, WeakReference weakReference) {
            this.f4587b = c1137a;
            this.f4586a = weakReference;
        }

        public void run() {
            this.f4587b.m8192a((Activity) this.f4586a.get());
        }
    }

    public void onActivityPaused(Activity activity) {
        if (!activity.isChangingConfigurations()) {
            WeakReference weakReference = new WeakReference(activity);
            Handler handler = this.mHandler;
            Runnable c1140d = new C1140d(this, weakReference);
            this.mRunnable = c1140d;
            handler.postDelayed(c1140d, delayTime);
        }
    }

    public void onActivityStarted(Activity activity) {
        this.mActivity = activity;
        if (this.mRunnable != null) {
            this.mHandler.removeCallbacks(this.mRunnable);
        }
        if (this.f4581f || activity == null || activity.isChangingConfigurations()) {
            Log.i(TAG, "still foreground");
            return;
        }
        this.f4581f = true;
        Log.w(TAG, "became foreground");
        this.f4583h.m8189a(f4578c);
    }

    public void onActivityStopped(Activity activity) {
        if (this.mRunnable != null) {
            this.mHandler.removeCallbacks(this.mRunnable);
        }
        m8192a(activity);
    }

    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    public void onActivityDestroyed(Activity activity) {
    }

    private void m8192a(Activity activity) {
        if (!this.f4581f) {
            Log.i(TAG, "still background");
        } else if (activity != this.mActivity || activity == null || activity.isChangingConfigurations()) {
            Log.i(TAG, "still foreground");
        } else {
            this.f4581f = false;
            Log.w(TAG, "went background");
            this.f4583h.m8189a(f4579d);
        }
    }
}

